package com.ruoyi.web.controller.business;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.business.domain.Cart;
import com.ruoyi.business.service.IWxCartService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 微信小程序购物车管理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/business/cart")
public class WxCartController extends BaseController
{
    @Autowired
    private IWxCartService wxCartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public AjaxResult list(Cart cart)
    {
        // 设置当前用户ID
        cart.setUserId(SecurityUtils.getUserId());
        List<Cart> list = wxCartService.selectCartList(cart);
        return AjaxResult.success(list);
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId)
    {
        return AjaxResult.success(wxCartService.selectCartById(cartId));
    }

    /**
     * 新增购物车
     */
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart)
    {
        // 设置当前用户ID
        cart.setUserId(SecurityUtils.getUserId());
        return toAjax(wxCartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart)
    {
        // 设置当前用户ID
        cart.setUserId(SecurityUtils.getUserId());
        return toAjax(wxCartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds)
    {
        return toAjax(wxCartService.deleteCartByIds(cartIds));
    }

    /**
     * 修改购物车商品数量
     */
    @PutMapping("/quantity")
    public AjaxResult updateQuantity(@RequestBody Cart cart)
    {
        // 设置当前用户ID
        cart.setUserId(SecurityUtils.getUserId());
        return toAjax(wxCartService.updateCartQuantity(cart));
    }
}